package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.RedisConstant;
import com.tbit.main.constant.SystemData;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.Account;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Country;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * 短信
 *
 * @author Leon
 * 2019年1月23日 下午3:51:50
 */
@RestController
@RequestMapping("/sms")
public class SmsController {
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private CountryService countryService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SmsService smsService;

    /**
     * 发送短信验证码
     */
    @RequestMapping("/send")
    public JsonResult send(Integer brandId, String country, String phone, HttpServletRequest request) {
        Assert.hasLength(phone, "AccountUser.phone.notNull");

        /**验证手机号码正确性*/
		/*if(!PhoneNumberUtil.isPhone(phone)){
			throw new BaseException("AccountUser.phone.format.error");
		}*/
        /** 国家代码 */
        if (country == null || "".equals(country)) {
            country = "86";
        }
        if (country.contains("+")) {
            country = country.replace("+", "");
        }

        Country countryDB = countryService.getById(country);

        /** 语言 */
        if (countryDB.getCode() == null || "".equals(countryDB.getCode())) {
            countryDB.setCode("zh");
        }

        Locale locale = new Locale("en", "US");
        if (countryDB.getCode().equalsIgnoreCase("zh")) {
            locale = new Locale("zh", "CN");
        } else if (countryDB.getCode().equalsIgnoreCase("hk")) {
            locale = new Locale("zh", "HK");
        }
        request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);

        if (countryDB == null) {
            throw new BaseException("CountryCode.notNull");
        }


        /**手机号码格式匹配*/
        String pattern = countryDB.getCountryPhoneRegex();
        boolean isMatch = Pattern.matches(pattern, phone);
        if (!isMatch) {
            throw new BaseException("Mobile.phone.format.error");
        }

        /**判断发短信的手机号码是否存在*/
        AccountUser accountUser = accountUserService.getByPhone(new AccountUser(country, phone, brandId));
        if (accountUser == null) {
            throw new BaseException("AccountUser.notExist");
        }

        String error = redisService.get(RedisConstant.SMS_MAIN_ERROR + phone);
        if (error != null) {
            throw new BaseException("SMS.ERROR.CODE");
        }


        /**获取验证码*/
        String code = StringUtil.getAuthCode();
        /**工具类发送验证码*/
        //XCSmsClient.sendAuthCode(phone, code);

        /** 获取品牌信息 */
        Account accountBrand = accountService.getByAccountAndType(accountUser.getAccountId(), 1);

        // 发送短信
        smsService.send(country, phone, accountBrand, code);

        /**添加验证码到内存,后面放到redis中*/
        SystemData.phoneAuthCodeMap.put(phone, code);

        return JsonResult.succ();
    }

}